package com.xyyl.patient.core.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @projectName:xyyl-patient-interface
 * @ClassName: AccessFilter
 * @Description: 跨域访问
 * @author chen
 * @date 2018年1月11日
 *
 */
public class AccessFilter implements Filter {
	private Logger logger = LogManager.getLogger(getClass());

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) res;
		HttpServletRequest request = (HttpServletRequest) req;
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");  
		response.setHeader("Access-Control-Allow-Credentials:", "true");
		String uri = ((HttpServletRequest) request).getPathInfo();
		logger.info("请求URI：{}，请求方式：{}", uri, request.getMethod());
		if (logger.isDebugEnabled()) {
			logger.info("请求参数：{}", request.getParameterMap());
		}
		filterChain.doFilter(req, res);

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		logger.info("-- system initialization completion! --");
	}

	@Override
	public void destroy() {
		logger.debug("--destroy--");
	}

}
